//
//  ClubMessageBoardVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/20.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubMessageBoardVC.h"
#import "AnswerMessBoardVC.h"
@interface ClubMessageBoardVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSArray *_responseArr;
}
@end

@implementation ClubMessageBoardVC

- (void)viewWillAppear:(BOOL)animated{
    [self requestForMessBoardList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"留言板";
    [self initTableView];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark RequestForMessBoardList
- (void)requestForMessBoardList{
    //参数
    NSDictionary *parameters = @{
                                 @"receiver": [NSString readToNSDefaultsWithKey:kClubId],
                                 @"pageNum": @"1",
                                 @"pageSize": @"20",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/bbs/list.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",dic);
        NSString *error = dic[@"code"];
        if (![dic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //获取成功
            _responseArr = dic[@"list"];
//            [SVProgressHUD showSuccessWithStatus:@"获取留言列表成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"获取留言列表失败"];
    }];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(10, 3, 50, 50)];
    icon.layer.masksToBounds = YES;
    icon.layer.cornerRadius = 5;
    NSString *iconStr = _responseArr[indexPath.row][@"senderPhoto"];
    if (iconStr == nil) {
        iconStr = [NSString readToNSDefaultsWithKey:kClubPhotoUrl];
    }
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYYMMddHHmmssSSS"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    iconStr = [NSString stringWithFormat:@"%@%@%@",iconStr, @"?",dateString];
    [icon sd_setImageWithURL:[NSURL URLWithString:iconStr] placeholderImage:[UIImage imageNamed:@"userIcon"]];
    [cell.contentView addSubview:icon];
    UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(70, 3, 100, 15)];
    nameLb.text = _responseArr[indexPath.row][@"senderName"];
    if (_responseArr[indexPath.row][@"senderName"] == nil) {
        nameLb.text = [NSString readToNSDefaultsWithKey:kClubName];
    }
    nameLb.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:nameLb];
    UILabel *timeLb = [[UILabel alloc]initWithFrame:CGRectMake(70, 35, 150, 15)];
    NSString *timeStr = _responseArr[indexPath.row][@"createTime"];
    timeStr = [timeStr substringToIndex:19];
    timeLb.text = timeStr;
    timeLb.font = [UIFont systemFontOfSize:13];
    timeLb.textColor = Gray_Color;
    [cell.contentView addSubview:timeLb];
    if (_responseArr[indexPath.row][@"senderName"] == nil) {
        //当没有发送者名字时，为俱乐部回复内容，不让其显示“回复”按钮
    }else{
        UIButton *answerBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 100, 0, 80, 40)];
        [answerBtn setTitle:@"回复" forState:UIControlStateNormal];
        [answerBtn addTarget:self action:@selector(answerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [answerBtn setTitleColor:Orange_Color forState:UIControlStateNormal];
        answerBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:answerBtn];
    }
    
    UILabel *contentLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, KScreenWidth - 20, 40)];
    contentLb.numberOfLines = 0;
    contentLb.font = [UIFont systemFontOfSize:11];
    contentLb.textColor = Gray_Color;
    contentLb.text = _responseArr[indexPath.row][@"content"];
    [cell.contentView addSubview:contentLb];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)answerBtnAction:(UIButton *)sender{
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *path = [_tableView indexPathForCell:cell];
    AnswerMessBoardVC *vc = [[AnswerMessBoardVC alloc]init];
    vc.originalID = _responseArr[path.row][@"id"];
    vc.originalName = _responseArr[path.row][@"senderName"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
